﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using Models;
using System.Web.Script.Serialization;

namespace Workflow
{
    /// <summary>
    /// Server 的摘要说明
    /// </summary>
    public class Server : IHttpHandler
    {
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        private UpLoadManager objUpLoadManager = new UpLoadManager();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string type = context.Request["Type"];
            try
            {
                if (type == "Submit")
                {
                    int teamId = Convert.ToInt32(context.Request["TeamId"]);
                    int roleId = Convert.ToInt32(context.Request["RoleId"]);
                    int workMould = Convert.ToInt32(context.Request["WorkMouldId"]);
                    List<UserInfo> userList = objUserTeamRoleManager.GetUserInfo(teamId, roleId, workMould);
                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    string s = jss.Serialize(userList);
                    context.Response.Write(s);
                }
                else if (type == "Close")
                {
                    int workParentId = Convert.ToInt32(context.Request["WorkParentId"]);
                    objWorkflowManager.UpdateWorkflowStatus(workParentId, -2);
                    context.Response.Write(workParentId.ToString());
                }
                else if (type == "Send")
                {
                    int workParentId = Convert.ToInt32(context.Request["WorkParentId"]);
                    int nodeNo = Convert.ToInt32(context.Request["NodeNo"]);
                    int childNodeNo = Convert.ToInt32(context.Request["ChildNo"]);
                    string userName = context.Request["UserName"];
                    WorkflowChildStatus objWorkflowChild = new WorkflowChildStatus()
                    {
                        StatusNo = 1,
                        WorkParentId = workParentId,
                        NodeNo = nodeNo,
                        ChildNodeNo = childNodeNo
                    };
                    objWorkflowManager.UpdateWorkflowStatus(userName, objWorkflowChild);
                    context.Response.Write("ok");
                }
                else if (type == "Return")
                {
                    //int workParentId = Convert.ToInt32(context.Request["WorkParentId"]);
                    //int workChildId = Convert.ToInt32(context.Request["WorkChildId"]);
                    //int parentStatusNo = Convert.ToInt32(context.Request["ParentStatusNo"]);
                    //int childStatusNo = Convert.ToInt32(context.Request["ChildStatusNo"]);
                    //WorkflowChildStatus objWorkflowChild = new WorkflowChildStatus()
                    //{
                    //    WorkParentId = workParentId,
                    //    WorkChildId = workChildId,
                    //    NodeNo = parentStatusNo
                    //};
                    //objWorkflowManager.ReturnWorkflow(workChildId, workParentId);
                    //context.Response.Write(workParentId.ToString());
                }
                else if (type == "Pause")
                {
                    int workParentId = Convert.ToInt32(context.Request["WorkParentId"]);
                    int workChildId = Convert.ToInt32(context.Request["WorkChildId"]);
                    //int parentStatusNo = Convert.ToInt32(context.Request["ParentStatusNo"]);
                    //int childStatusNo = Convert.ToInt32(context.Request["ChildStatusNo"]);
                    //WorkflowChildStatus objWorkflowChild = new WorkflowChildStatus()
                    //{
                    //    WorkParentId = workParentId,
                    //    WorkChildId = workChildId,
                    //};
                    objWorkflowManager.PauseWorkflow(workChildId,workParentId);
                    context.Response.Write(workParentId.ToString());
                }
                else if (type == "Pass")
                {
                    int workParentId = Convert.ToInt32(context.Request["WorkParentId"]);
                    int workChildId = Convert.ToInt32(context.Request["WorkChildId"]);
                    int parentStatusNo = Convert.ToInt32(context.Request["ParentStatusNo"]);
                    int childStatusNo = Convert.ToInt32(context.Request["ChildStatusNo"]);
                    int isSingleNode = Convert.ToInt32(context.Request["IsSingleNode"]);
                    int childNodeNo = Convert.ToInt32(context.Request["ChildNodeNo"]);
                    int nodeCount = Convert.ToInt32(context.Request["NodeCount"]);
                    if (isSingleNode == -1 && childNodeNo != 1)//多结节点且不是第一个子节点
                    {
                        WorkflowChildStatus objWorkflowChild = new WorkflowChildStatus()
                        {
                            WorkChildId = workChildId,
                            WorkParentId = workParentId
                        };
                        objWorkflowManager.UpdateWorkflowStatus(objWorkflowChild);
                        context.Response.Write(workParentId.ToString());
                    }
                    else if (parentStatusNo == nodeCount)//最后一个节点
                    {
                        WorkflowChildStatus objWorkflowChild = new WorkflowChildStatus()
                        {
                            WorkChildId = workChildId,
                            WorkParentId = workParentId
                        };
                        objWorkflowManager.UpdateWorkflowStatus(objWorkflowChild);
                        context.Response.Write(workParentId.ToString());
                    }
                    else
                    {
                        WorkflowStatus objWorkflow = new WorkflowStatus()
                        {
                            WorkParentId = workParentId,
                            WorkChildId = workChildId,
                            StatusNo = parentStatusNo,
                            ChildStatusNo = childNodeNo,
                            IsSingleNode = isSingleNode,
                            ChildNodeNo = childNodeNo
                        };
                        JavaScriptSerializer jss = new JavaScriptSerializer();
                        string s = jss.Serialize(objWorkflow);
                        context.Response.Write(s);
                    }
                }
                else if (type == "DownLoad")
                {
                    string fileAddress = context.Request["FileAddress"].ToString();
                    string fileName = context.Request["Filename"].ToString();
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
                    context.Response.TransmitFile(fileAddress);
                    context.Response.End();
                }
                else if (type == "Read")
                {
                    int workChildId = Convert.ToInt32(context.Request["WorkChildId"]);
                    objWorkflowManager.UpdateWorkflowChildRead(workChildId);
                    context.Response.Write("ok");
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                context.Response.Write(ex.Message.ToString());
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}